#include "logger.h"
#include <windows.h>
using namespace std;

int read_ip_port_ini(char *pFile,char *pName,string& ip, int& port)
{
    char buffer[1024] = {};
    port  = GetPrivateProfileIntA(pName, "port", -1, pFile);
    GetPrivateProfileStringA(pName, "ip", "", buffer, sizeof(buffer) / sizeof(char), pFile);
    ip = string(buffer);
    return 0;
}

static bool checkLogDir()
{
    QDir dir("Log");
    QDir tmp;
    if(!dir.exists())
    {
        tmp.mkdir("Log");
    }
    dir = QDir("Log/info");
    if(!dir.exists()){
        tmp.mkdir("Log/info");
    }

    dir = QDir("Log/warning");
    if(!dir.exists()){
        tmp.mkdir("Log/warning");
    }

    dir = QDir("Log/error");
    if(!dir.exists()){
        tmp.mkdir("Log/error");
    }

    dir = QDir("Log/fatal");
    if(!dir.exists()){
        tmp.mkdir("Log/fatal");
    }

    return true;
}

int init_log()
{
    //glog初始化
    checkLogDir();
    google::SetLogDestination(google::GLOG_INFO, "Log/info/");
    google::SetLogDestination(google::GLOG_WARNING, "Log/warning/");
    google::SetLogDestination(google::GLOG_ERROR, "Log/error/");
    google::SetLogDestination(google::GLOG_FATAL, "Log/fatal/");
    google::SetStderrLogging(google::GLOG_INFO);
    google::SetLogFilenameExtension(".log");
    google::InitGoogleLogging("chargeRobot.exe");
    FLAGS_logbufsecs = 0;
    FLAGS_max_log_size = 10;
    return 0;
}

string bufToHexStr(const char *pBuf, int bufLen)
{
    if(NULL == pBuf || bufLen < 0)
    {
        return "";
    }
    int i = 0;
    string sRet = "";
    for(i = 0; i < bufLen; i++)
    {
        char szTmp[3] = {0};
        sprintf(szTmp, "%02x ", pBuf[i]&0xff);
        sRet += szTmp;
    }
    return sRet;
}
